package com.ruoyi.ca.controller;

import com.ruoyi.ca.domain.CaFpxx;
import com.ruoyi.ca.service.ICaFpxxService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发票信息Controller
 *
 * @author ycong
 * @date 2024-04-13
 */
@RestController
@RequestMapping("/ca/fpxx")
public class CaFpxxController extends BaseController {
    @Autowired
    private ICaFpxxService caFpxxService;

    /**
     * 查询发票信息列表
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:list')")
    @GetMapping("/list")
    public TableDataInfo list(CaFpxx caFpxx) {
        startPage();
        List<CaFpxx> list = caFpxxService.selectCaFpxxList(caFpxx);
        return getDataTable(list);
    }

    /**
     * 导出发票信息列表
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:export')")
    @Log(title = "发票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaFpxx caFpxx) {
        List<CaFpxx> list = caFpxxService.selectCaFpxxList(caFpxx);
        ExcelUtil<CaFpxx> util = new ExcelUtil<CaFpxx>(CaFpxx.class);
        util.exportExcel(response, list, "发票信息数据");
    }

    /**
     * 获取发票信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(caFpxxService.selectCaFpxxById(id));
    }

    /**
     * 新增发票信息
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:add')")
    @Log(title = "发票信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaFpxx caFpxx) {
        return toAjax(caFpxxService.insertCaFpxx(caFpxx));
    }

    /**
     * 修改发票信息
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:edit')")
    @Log(title = "发票信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaFpxx caFpxx) {
        return toAjax(caFpxxService.updateCaFpxx(caFpxx));
    }

    /**
     * 删除发票信息
     */
    @PreAuthorize("@ss.hasPermi('ca:fpxx:remove')")
    @Log(title = "发票信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(caFpxxService.deleteCaFpxxByIds(ids));
    }

    @Log(title = "发票信息导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ca:fpxx:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        if (null == file) {
            return AjaxResult.error("请选择要导入的文件！");
        }
        String name = file.getOriginalFilename();
        if (StringUtils.isEmpty(name) || !name.endsWith("zip")) {
            return AjaxResult.error("仅允许导入zip格式文件！");
        }
        String operName = getUsername();
        String message = caFpxxService.importCaFpxx(file, operName);
        return success(message);
    }

    /**
     * 发票下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/downLoad")
    public void downLoad(String fileName, HttpServletResponse response) {
        caFpxxService.downLoad(fileName, response);
    }

    /**
     * 查询发票信息列表
     */
    @GetMapping("/queryByGfmc")
    public TableDataInfo queryByGfmc(String jylsh) {

        if (StringUtils.isEmpty(jylsh)) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setMsg("请输入交易流水号");
            return tableDataInfo;
        }

        startPage();
        List<CaFpxx> list = caFpxxService.queryFpxxListByJylsh(jylsh);
        return getDataTable(list);
    }

    /**
     * 查询发票信息列表
     */
    @GetMapping("/queryGfmc")
    public List<String> queryGfmc(CaFpxx caFpxx) {
        List<String> list = caFpxxService.queryGfmc(caFpxx.getGfmc());
        return list;
    }
}
